<?php
namespace Todo\Model\Sys;

class DictModel extends SysBaseModel
{
    protected $_cache = false;

    protected $_validate = array(
        array('title', 'require', '请输入字典名称', 1),
        array('code', 'require', '请输入字典代码', 1),
    );

    public function findAll($map=false){
        $list = $this->cache($this->_cache.'_list')->select();
        if($map){
            $maps = array();
            foreach ($list as $row){
                $maps[$row['code']] = $this->mapValue($row['value']);
            }
            return $maps;
        }
        return $list;
    }
    /**
     * 将一行一条的键值对形式变成关联数组k1=v1
     * @param string $content 内容
     * @return array|bool
     */
    public function mapValue($content){
        $rows = explode("\r", $content);
        if(!$rows) return false;
        $map = array();
        foreach ($rows as $row){
            if(!trim($row)) continue;
            $rd = explode('=', $row);
            $map[trim($rd[0])] = trim($rd[1]);
        }
        return  $map;
    }

    protected function _after_insert($data,$options){
        S($this->_cache.'_list', null);
    }
    protected function _after_update($data,$options){
        S($this->_cache.'_list', null);
    }
    protected function _after_delete($data,$options){
        S($this->_cache.'_list', null);
    }
}